//
//  CQMineMainViewController.m
//  HiChat
//
//  Created by wqiong on 16/1/10.
//  Copyright © 2016年 wqiong. All rights reserved.
//

#import "CQMineMainViewController.h"
#import "CQLoginViewController.h"
#import "CQMineLoginTableViewCell.h"
#import "CQMineLoginHeadTableViewCell.h"
#import "CQNicknameViewController.h"
#import "CQMineLoginViewController.h"
@interface CQMineMainViewController ()<UITableViewDelegate, UITableViewDataSource, returnNicknameDelegate>


@property (nonatomic, retain) NSString *nickname;

@property (nonatomic, retain) UITableView *tableV;

@end

@implementation CQMineMainViewController
- (void)dealloc
{
    [_nickname release];
    [_tableV release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarController.tabBar.hidden = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //判断rightBar是注销还是登录
    NSUserDefaults *useD = [NSUserDefaults standardUserDefaults];
    NSString *getName = [useD objectForKey:@"userName"];
    
//    if (getName == nil) {
////        self.mineView = [[CQMineMainView alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height)];
////        self.mineView.delegate = self;
////        [self.view addSubview:self.mineView];
////        [_mineView release];
//        
//    } else {
        self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
        self.tableV.delegate = self;
        self.tableV.dataSource = self;
        [self.tableV registerClass:[CQMineLoginTableViewCell class] forCellReuseIdentifier:@"cell"];
        [self.tableV registerClass:[CQMineLoginHeadTableViewCell class] forCellReuseIdentifier:@"cell1"];
        [self.view addSubview:self.tableV];
        [_tableV release];
        
        
        UIButton *cancelBut = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBut.frame = CGRectMake(0, 0, 120, 50);
        cancelBut.backgroundColor = [UIColor lightGrayColor];
        [cancelBut setTitle:@"注   销" forState:UIControlStateNormal];
        cancelBut.titleLabel.textAlignment = NSTextAlignmentCenter;
        cancelBut.titleLabel.font = [UIFont systemFontOfSize:20.0];
        cancelBut.layer.cornerRadius = 15.0;
        cancelBut.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0 + 100);
        [cancelBut addTarget:self action:@selector(cancelButAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:cancelBut];
//    }
    [self setback];
}
- (void)setback {
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageV.image = [UIImage imageNamed:@"loginBack"];
    [self.tableV setBackgroundView:imageV];
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [imageV release];
}
- (void)cancelButAction {
//
//    CATransition *transition = [CATransition animation];
//    transition.duration = 5.0;
//    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//    transition.type = @"rippleEffect";
//    transition.subtype = kCATransitionFromRight;
//    transition.delegate = self;
//    [self.navigationController.view.layer addAnimation:transition forKey:nil];

    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
        if (error && error.errorCode != EMErrorServerNotLogin) {
            NSLog(@"%@", error.description);
        }
        else{
            NSLog(@"zzzzzzzzzzzzzzzzzzz");
            NSUserDefaults *userName = [NSUserDefaults standardUserDefaults];
            [userName setObject:nil forKey:@"userName"];
            
            CQMineLoginViewController *viewC = [CQMineLoginViewController shareMineLogInVC];
//            [self.tabBarDelegate tabBarHidden];
//            self.hidesBottomBarWhenPushed = YES;
            [self presentViewController:viewC animated:NO completion:^{
                
            }];
        }
        
    } onQueue:nil];
    
    [self.tableV reloadData];
}
-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationItem.title = @"我的";
    
    [self.tableV reloadData];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    
}

- (void)returnNickname:(NSString *)nickname {
    self.nickname = nickname;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self returnNickname:self.nickname];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            return 80;
            break;
        case 1:
            return 50;
            break;
        case 2:
            return 50;
            break;
        case 3:
            return 50;
            break;
            
        default:
            break;
    }
    return 50;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (2 == indexPath.row) {
        CQNicknameViewController *nicknameVC = [[CQNicknameViewController alloc] init];
        nicknameVC.delegate = self;
        [self.navigationController pushViewController:nicknameVC animated:YES];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        CQMineLoginHeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.labelUser.text = @"至尊宝";
        cell.imageV.image = [UIImage imageNamed:@"touxiang1"];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    } else {
        CQMineLoginTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (1 == indexPath.row) {
            cell.label.text = @"账  号";
            cell.labelUser.text = @"XXXXXX";
        }
        if (2 == indexPath.row) {
            cell.label.text = @"昵  称";
            
            //反归档昵称
            //取出用户名
            NSUserDefaults *useD = [NSUserDefaults standardUserDefaults];
            NSString *getName = [useD objectForKey:@"userName"];
            
            NSString *nickName = [NSString stringWithFormat:@"nickname%@", getName];
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path stringByAppendingPathComponent:nickName]];
            if (result == nil) {
                cell.labelUser.text = @"XXXXXX";
            } else {
                 cell.labelUser.text = result;
            }
        }
        if (3 == indexPath.row) {
            cell.label.text = @"等  级";
            cell.labelUser.text = @"XXXXXX";
        }
        if (4 == indexPath.row) {
            cell.label.text = @"性  别";
            cell.labelUser.text = @"MAN";
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }
}
@end
